package org.dim.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.SneakyThrows;
import org.dim.oa.mapper.SignMapper;
import org.dim.oa.po.ServerResponse;
import org.dim.oa.po.SignEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetAddress;
import java.util.Date;
import java.util.List;

/**
 * Created by lmj on 2021/3/19.
 */
@RestController
@RequestMapping
public class SignInController {
    @Autowired
    SignMapper signMapper;
    @SneakyThrows
    @RequestMapping("/signIn")
    public ServerResponse sign(@RequestBody SignEntity signEntity){
        InetAddress address = InetAddress.getLocalHost();
        signEntity.setSignIp(address.getHostAddress());
        signEntity.setSignPlatform(address.getHostName());
        signEntity.setSignTime(new Date());
        /**
         * mp自带的insert方法,新增成功后会自动将自增的id注入到实体类中
         */
        signMapper.insert(signEntity);
        return ServerResponse.success(signEntity);
    }

    @RequestMapping("/signHis")
    public ServerResponse signHis(@RequestBody SignEntity signEntity){

        //   InetAddress address = InetAddress.getLocalHost();
//        signEntity.setSignIp(address.getHostAddress());
//        signEntity.setSignPlatform(address.getHostName());
//        signEntity.setSignTime(new Date());
        QueryWrapper<SignEntity> signEntityQueryWrapper = new QueryWrapper<>();
        List<SignEntity> signEntitys = signMapper.selectList(signEntityQueryWrapper.in("user_id", signEntity.getUserId()));
        return ServerResponse.success(signEntitys);
    }

    @RequestMapping("/getDay")
    public ServerResponse getDay(@RequestBody SignEntity signEntity){

        //   InetAddress address = InetAddress.getLocalHost();
//        signEntity.setSignIp(address.getHostAddress());
//        signEntity.setSignPlatform(address.getHostName());
//        signEntity.setSignTime(new Date());
        QueryWrapper<SignEntity> signEntityQueryWrapper = new QueryWrapper<>();
        List<SignEntity> signEntitys = signMapper.selectList(signEntityQueryWrapper.in("user_id", signEntity.getUserId()));
        return ServerResponse.success(signEntitys.size());
    }
}
